package com.polaris.live.popup

import android.content.Context
import android.os.CountDownTimer
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.ScreenUtils
import com.blankj.utilcode.util.StringUtils
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.core.CenterPopupView
import com.polaris.live.R
import com.polaris.live.common.cacha.CacheUtil
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.constant.CacheConst
import com.polaris.live.common.constant.CacheConst.country
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.databinding.DialogCallGameBalanceBinding
import com.polaris.live.im.sfs.bean.live.ext.UserBasicVo
import com.polaris.live.im.sfs.bean.live.payload.CallGameStatusPayloadDto
import com.polaris.live.translation.R.string
import com.polaris.live.widget.avatar.FrameImageUtils

/**
 * 通话游戏结算
 * @author Created by 天晴 on 2024/4/23 19:13
 * @since 1.0.0
 **/
class CallGameBalancePopup(
    context: Context,
    private val mUserBasicVo: UserBasicVo,
    private val mCallGameBean: CallGameStatusPayloadDto,
    private val onNext: (() -> Unit)? = null,
) : CenterPopupView(context) {

    override fun getImplLayoutId(): Int {
        return R.layout.dialog_call_game_balance
    }

    private lateinit var mBinding: DialogCallGameBalanceBinding

    private val mTime: TimeCount by lazy {
        TimeCount(10 * 1000, 1000)
    }

    override fun onCreate() {
        super.onCreate()
        mBinding = DialogCallGameBalanceBinding.bind(popupImplView)
        mTime.start()
        val win = mCallGameBean.winner == UserManager.getUserId()
        mBinding.apply {
            titleTv.text =
                if (win) StringUtils.getString(string.you_win) else StringUtils.getString(string.you_lose)
            titleTv.textColorBuilder.apply {
                setTextGradientColors(
                    ColorUtils.getColor(R.color.white),
                    if (win) ColorUtils.getColor(R.color.color_e5b90c) else ColorUtils.getColor(R.color.color_42b0ff)
                ).intoTextColor()
            }

            UserManager.getUser().apply {
                av1.setImageUrl(avatar, userId)
                avFv1.setFrameImage(FrameImageUtils.urlToFrameImageBean(decoration?.avatarFrame))
                tvName1.text = nickname
                cvCountry1.setCountry(country)
                saSex1.setSexAndAge(sex, birthday)
                vipView.setVipLevel(profile?.vipLevel)
                level1.setRichCharmBySexLevel(
                    sex,
                    account?.richs,
                    account?.charms
                )
                tvBan1.text =
                    if (win) StringUtils.getString(string.win) else StringUtils.getString(string.lose)
                tvBan1.textColorBuilder.apply {
                    setTextGradientColors(
                        ColorUtils.getColor(R.color.white),
                        if (win) ColorUtils.getColor(R.color.color_e5b90c) else ColorUtils.getColor(
                            R.color.color_42b0ff
                        )
                    ).intoTextColor()

                }.intoTextColor()
            }
            mUserBasicVo.apply {
                av2.setImageUrl(avatar, userId)
                avFv2.setFrameImage(FrameImageUtils.urlToFrameImageBean(avatarFrame))
                tvName2.text = nickname
                cvCountry2.setCountry(CacheConst.country)
                saSex2.setSexAndAge(sex, birthday)
                vipView2.setVipLevel(vipLevel)
                level2.setRichCharmBySexLevel(
                    sex,
                    richs,
                    charms
                )
                tvBan2.text =
                    if (win) StringUtils.getString(string.lose) else StringUtils.getString(string.win)
                tvBan2.textColorBuilder.apply {
                    setTextGradientColors(
                        ColorUtils.getColor(R.color.white),
                        if (win) ColorUtils.getColor(R.color.color_42b0ff) else ColorUtils.getColor(
                            R.color.color_e5b90c
                        )
                    ).intoTextColor()
                }.intoTextColor()
            }
            cancel.setOnSingleClickListener {
                dismiss()
                onNext?.invoke()
            }
        }
    }

    private inner class TimeCount(millisInFuture: Long, countDownInterval: Long) :
        CountDownTimer(millisInFuture, countDownInterval) {

        override fun onTick(millisUntilFinished: Long) {
            mBinding.countdownTv.text =
                StringUtils.getString(string.countdown_tips, millisUntilFinished / 1000)
        }

        override fun onFinish() {
            dismiss()
        }

    }

    override fun getMaxWidth(): Int {
        return ScreenUtils.getScreenWidth()
    }

    override fun getPopupWidth(): Int {
        return ScreenUtils.getScreenWidth()
    }

    override fun onDestroy() {
        super.onDestroy()
        mTime.cancel()
    }

    companion object {

        fun show(
            context: Context,
            mUserBasicVo: UserBasicVo,
            mCallGameBean: CallGameStatusPayloadDto,
            onNext: (() -> Unit)? = null,
        ): BasePopupView {
            //存弹窗时间
            CacheUtil.setLastGameWinPopTime(CacheUtil.getServerTime())
            return XPopup.Builder(context)
                .hasNavigationBar(false)
                .isDestroyOnDismiss(true)
                .asCustom(CallGameBalancePopup(context, mUserBasicVo, mCallGameBean, onNext))
                .show()
        }
    }
}